//
//  MyViewController.m
//  NewMusic
//
//  Created by dllo on 15/11/11.
//  Copyright © 2015年 Going. All rights reserved.
//

#import "MyViewController.h"
#import "MyButtonTableViewCell.h"
#import "LoginTableViewCell.h"
#import "MyMusicTableViewCell.h"
#import "GuessLoveTableViewCell.h"
#import "PlayerViewController.h"
#import "BaseClassNavigationController.h"
#import "AVPlayerViewController.h"
#import "CallMeViewController.h"
#import "moodViewController.h"
#import "ListenSongssViewController.h"
@interface MyViewController () <UITableViewDelegate, UITableViewDataSource, changeViewInButtonCell, changeViewInLoginCell>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *musicArray;
@property (nonatomic, retain) UISearchController *searchController;
@end

@implementation MyViewController

- (void)dealloc
{
    [_tableView release];
    [_musicArray release];
    [_searchController release];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"pushTocallMe" object:nil];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.musicArray = [NSMutableArray array];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushToCallMeViewController) name:@"pushTocallMe" object:nil];
    // 创建tableView
    [self creatTableView];
}

// 行数返回
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (0 == section)
    {
        return 3;
    }
    return self.musicArray.count + 1;
}
// 创建cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (0 == indexPath.section)
    {
        if (0 == indexPath.row)
        {
            static NSString *cellLogin = @"loginCell";
            LoginTableViewCell *loginCell = [tableView dequeueReusableCellWithIdentifier:cellLogin];
            if (nil == loginCell)
            {
                loginCell = [[[LoginTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellLogin] autorelease];
                loginCell.selectionStyle = UITableViewCellSelectionStyleNone;
                loginCell.delegate = self;
            }
            return loginCell;
        } else if (1 == indexPath.row) {
            static NSString *cellIdent = @"ButtonCell";
            MyButtonTableViewCell *ButtonCell = [tableView dequeueReusableCellWithIdentifier:cellIdent];
            if (nil == ButtonCell)
            {
                ButtonCell = [[[MyButtonTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdent]autorelease];
                ButtonCell.selectionStyle = UITableViewCellSelectionStyleNone;
                ButtonCell.backgroundColor = [UIColor clearColor];
                ButtonCell.delegate = self;
            }
            return ButtonCell;
        } else {
            static NSString *cellGuess = @"GuessCell";
            GuessLoveTableViewCell *GuessCell = [tableView dequeueReusableCellWithIdentifier:cellGuess];
            if (nil == GuessCell)
            {
                GuessCell = [[[GuessLoveTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellGuess] autorelease];
                GuessCell.selectionStyle = UITableViewCellSelectionStyleNone;
                GuessCell.backgroundColor = [UIColor clearColor];
            }
            return GuessCell;
        }
    } else {
        static NSString *music = @"musicCell";
        MyMusicTableViewCell *musicCell = [tableView dequeueReusableCellWithIdentifier:music];
        if (nil == musicCell)
        {
            musicCell = [[[MyMusicTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:music] autorelease];
            musicCell.backgroundColor = [UIColor clearColor];
            musicCell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        if (self.musicArray.count != indexPath.row)
        {
            musicCell.str = self.musicArray[indexPath.row];
        }
        return musicCell;
    }
}
// 行高返回
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (0 == indexPath.section)
    {
        if (0 == indexPath.row)
        {
            return 60;
        } else if (1 == indexPath.row) {
            return 300;
        } else {
            return 70;
        }
    }
    return 55;
}
// 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (1 == indexPath.section)
    {
        if (self.musicArray.count == indexPath.row)
        {
            UIAlertController *controller = [UIAlertController alertControllerWithTitle:nil message:@"输入你要创建歌单名称" preferredStyle:UIAlertControllerStyleAlert];
            [controller addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
            }];
            UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            }];
            UIAlertAction *actionSure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                NSString *str = controller.textFields.lastObject.text;
                [self.musicArray insertObject:str atIndex:0];
                NSIndexPath *index = [NSIndexPath indexPathForItem:0 inSection:1];
                [self.tableView beginUpdates];
                [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:index, nil] withRowAnimation:UITableViewRowAnimationRight];
                [self.tableView endUpdates];
            }];
            [controller addAction:actionCancel];
            [controller addAction:actionSure];
            UIView *view = controller.view.subviews[0];
            view.alpha = 0.9;
            dispatch_async(dispatch_get_main_queue(), ^{
                [self presentViewController:controller animated:YES completion:^{
                }];
            });
            
        }
    }else if (indexPath.section == 0) {
        moodViewController *view = [[moodViewController alloc]init];
        [self.navigationController pushViewController:view animated:YES];
        [view release];
    }
}
// 标题高度返回
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (0 == section)
    {
        return 0;
    }
    return 40;
}
// 列数返回
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
// 标题字返回
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (0 == section)
    {
        return nil;
    }
    return @"我的歌单";
}
// 标题返回
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    UIView *viewBackGroud = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 40)];
    viewBackGroud.backgroundColor = [UIColor whiteColor];
    viewBackGroud.alpha = 0.05;
    [view addSubview:viewBackGroud];
    [viewBackGroud release];
    view.tintColor = [UIColor clearColor];
    UITableViewHeaderFooterView *headView = (UITableViewHeaderFooterView *)view;
    headView.textLabel.textColor = [UIColor whiteColor];
}
// 创建
- (void)creatTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 547) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.tableView];
    [_tableView release];
}
#pragma 跳转下载等界面
- (void)changeViewInButtonCell:(NSInteger)num
{
    if (num == 5) {
        ListenSongssViewController *listen = [[ListenSongssViewController alloc] init];
        [self.navigationController pushViewController:listen animated:YES];
    }
}
#pragma 跳转登陆界面
- (void)changeViewInLoginCell
{
    
}
- (void)pushToCallMeViewController
{
    CallMeViewController *callMe = [CallMeViewController new];
    [self.navigationController pushViewController:callMe animated:YES];
    [callMe release];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
